import 'package:cached_network_image/cached_network_image.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';

class ImageUtils {
  static ImageProvider getAssetImage(String name,
      {ImageFormat format = ImageFormat.png}) {
    return AssetImage(getImagePath(name, format: format));
  }

  static String getImagePath(String name,
      {ImageFormat format = ImageFormat.png}) {
    return 'assets/res/$name.${format.value}';
  }

  static ImageProvider getImageProvider(String? imageUrl,
      {String holderImg = 'none'}) {
    if (TextUtil.isEmpty(imageUrl) || !imageUrl!.startsWith('http')) {
      return getAssetImage(imageUrl!);
    }
    return CachedNetworkImageProvider(imageUrl);
  }
}

enum ImageFormat { png, jpg, gif, webp }

extension ImageFormatExtension on ImageFormat {
  String get value => ['png', 'jpg', 'gif', 'webp'][index];
}
